/** * AJAX Search Script for Personnel Certification * Displays certification fields in table format */ (function($) { 'use strict'; $(document).ready(function() { // Handle search form submission $('#pc-search-form').on('submit', function(e) { e.preventDefault(); var searchQuery = $('#pc-search-input').val().trim(); if (searchQuery === '') { alert('Please enter a search term'); return; } // Show preloader $('#pc-preloader').fadeIn(); $('#pc-results-container').hide(); $('#pc-no-results').hide(); // AJAX request $.ajax({ url: pcAjax.ajax_url, type: 'POST', data: { action: 'pc_search_certification', nonce: pcAjax.nonce, search_query: searchQuery }, success: function(response) { // Hide preloader $('#pc-preloader').fadeOut(); if (response.success) { displayResults(response.data.results, response.data.count); } else { showNoResults(response.data.message); } }, error: function() { $('#pc-preloader').fadeOut(); alert('An error occurred. Please try again.'); } }); }); /** * Display search results in comprehensive table format */ function displayResults(results, count) { // Build results counter var counterHtml = '
'; counterHtml += '
'; counterHtml += '' + count + ' '; counterHtml += count === 1 ? 'certification found' : 'certifications found'; counterHtml += '
'; counterHtml += '
'; // Build table with horizontal scroll wrapper var tableHtml = '
'; tableHtml += '
'; tableHtml += ''; // Table header - 7 FIELDS (as specified in requirements) tableHtml += ''; tableHtml += ''; tableHtml += ''; tableHtml += ''; tableHtml += ''; tableHtml += ''; tableHtml += ''; tableHtml += ''; tableHtml += ''; tableHtml += ''; tableHtml += ''; // Table body tableHtml += ''; $.each(results, function(index, item) { tableHtml += ''; // Client ID tableHtml += ''; // Name tableHtml += ''; // Certification Scheme tableHtml += ''; // Certification No tableHtml += ''; // Original Date tableHtml += ''; // Decision Date tableHtml += ''; // Expiry Date /*tableHtml += '';*/ tableHtml += ''; tableHtml += ''; }); tableHtml += ''; tableHtml += '
Client IDNameCertification ScopeCertification NoOriginal DateDecision DateExpiry Date
'; tableHtml += '' + escapeHtml(item.client_id) + ''; tableHtml += ''; tableHtml += '
' + escapeHtml(item.name) + '
'; tableHtml += '
'; if (item.certification_scheme) { tableHtml += '' + escapeHtml(item.certification_scheme) + ''; } else { tableHtml += '—'; } tableHtml += ''; tableHtml += '' + escapeHtml(item.certification_no) + ''; tableHtml += ''; tableHtml += '' + (item.original_date ? escapeHtml(item.original_date) : '—') + ''; tableHtml += ''; tableHtml += '' + (item.decision_date ? escapeHtml(item.decision_date) : '—') + ''; tableHtml += ''; if (item.expiry_date) { tableHtml += ''; tableHtml += escapeHtml(item.expiry_date); tableHtml += ''; } else { tableHtml += '—'; } tableHtml += ''; tableHtml += '' + (item.expiry_date ? escapeHtml(item.expiry_date) : '—') + ''; tableHtml += '
'; tableHtml += '
'; // close pc-table-wrapper tableHtml += '
'; // close pc-table-scroll // Combine counter and table var fullHtml = counterHtml + tableHtml; // Display results $('#pc-results-container').html(fullHtml).fadeIn(); $('#pc-no-results').hide(); } /** * Show no results message */ function showNoResults(message) { $('#pc-results-container').hide(); var html = '
🔍
'; html += '

No certifications found

'; html += '

' + (message || 'Try a different search term') + '

'; $('#pc-no-results').html(html).fadeIn(); } /** * Escape HTML to prevent XSS */ function escapeHtml(text) { if (!text) return ''; var map = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }; return String(text).replace(/[&<>"']/g, function(m) { return map[m]; }); } /** * Clear search results */ $('#pc-clear-search').on('click', function(e) { e.preventDefault(); $('#pc-search-input').val(''); $('#pc-results-container').empty().hide(); $('#pc-no-results').hide(); }); }); })(jQuery);